package com.fanbai.score.common.base;

import android.app.Application;

import com.fanbai.score.common.Utils.AppUtils;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.SPUtils;
import com.fanbai.score.common.Utils.log.YD_Log;
import com.fanbai.score.user.bean.CartGoodsInfo_Bean;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.MemoryCookieStore;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/3/6.
 *
 * @author lizhipei
 */

public class MyApplication extends Application
{
    private static MyApplication myApplication;
    @Override
    public void onCreate()
    {
        super.onCreate();
//        SQLiteDatabase db = Connector.getDatabase();
//        CrashReport.initCrashReport(getApplicationContext(), "900059140", true);
//        Bugly.init(getApplicationContext(), "900059140", false);
        myApplication = this;
        YD_Log.isDebug = true;
        AppUtils.init(myApplication);
        cookieStore = new MemoryCookieStore();
        initHttpUtils();
    }

    public static MyApplication getInstanti(){
        return myApplication;
    }

    private MemoryCookieStore cookieStore ;
    /**
     * 初始化OkhttpUtils框架
     */
    private void initHttpUtils() {
        // 设置OkhttpUtils请求https时的证书
//		 HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null,
//		 null, null);
        // 初始化OkhttpUtils

        CookieJarImpl cookieJar = new CookieJarImpl(cookieStore);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new LoggerInterceptor("OkHttpUtils"))// log调试
                // https链接使用证书的配置
                // .sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
                // 连接超时配置
                .connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
                // 其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    public MemoryCookieStore getCookieStore() {
        return cookieStore;
    }

    //-----------------------------------------------------------------------------------------------------------
    /**
     * 添加购物车
     * @param bean
     */
    public void addGoodsToUserCart(CartGoodsInfo_Bean bean){
        String goodsListStr = SPUtils.with(this).get(SPUtils.KEY_USER_CARTGOODS, "[]");
        List<CartGoodsInfo_Bean> beans = JsonUtils.toArr(goodsListStr, CartGoodsInfo_Bean[].class);
        if(beans == null || beans.size() <= 0){
            beans = new ArrayList<CartGoodsInfo_Bean>();
        }else if(beans.size()> 0){
            beans = new ArrayList<>(beans);
            for (int i = beans.size()-1; i >= 0; i--){
                if(beans.get(i).getGoodsPid().equals(bean.getGoodsPid())){
                    beans.remove(i);
                }
            }
        }
        beans.add(bean);
        SPUtils.with(this).save(SPUtils.KEY_USER_CARTGOODS, JsonUtils.toJsonObj(beans));
    }

    /**
     * 获取购物车商品
     * @return
     */
    public List<CartGoodsInfo_Bean> getUserCartGoods(){
        String goodsListStr = SPUtils.with(this).get(SPUtils.KEY_USER_CARTGOODS, "[]");
        List<CartGoodsInfo_Bean> beans = JsonUtils.toArr(goodsListStr, CartGoodsInfo_Bean[].class);
        if(beans == null){
            beans = new ArrayList<>();
        }
        return beans;
    }

    /**
     * 清空购物车
     */
    public void removeAllUserCart(){
        SPUtils.with(this).save(SPUtils.KEY_USER_CARTGOODS, "[]");
    }

}
